package telas;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;

import DAOs.DAOUsuario;
import classes.Usuario;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class ConsultaUsuario extends JFrame {

	private JPanel contentPane;
	private JTextField txtUsername;
	private JComboBox cbbUsername;
	private JButton btnProcurar;
	
	private DAOUsuario dadosUsuario = new DAOUsuario();
	private CadastraUsuario cadastraUsuario = new CadastraUsuario();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConsultaUsuario frame = new ConsultaUsuario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConsultaUsuario() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 475, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 11, 77, 14);
		contentPane.add(lblUsername);
		
		txtUsername = new JTextField();
		txtUsername.setColumns(10);
		txtUsername.setBounds(10, 36, 132, 20);
		contentPane.add(txtUsername);
		
		btnProcurar = new JButton("Procurar");
		btnProcurar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Usuario> usuarios = new ArrayList();
				usuarios = dadosUsuario.searchUsuario(txtUsername.getText());
				cbbUsername.removeAllItems();
				
				for (int i=0; i<usuarios.size();i++){
				cbbUsername.addItem(usuarios.get(i));
			}
			}
		});
		btnProcurar.setBounds(169, 35, 89, 23);
		contentPane.add(btnProcurar);
		
		cbbUsername = new JComboBox();
		cbbUsername.setBounds(288, 36, 155, 20);
		contentPane.add(cbbUsername);
		
		JButton button_1 = new JButton("Novo Registro");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				cadastraUsuario.AbreTela(1, null);
				cadastraUsuario.setVisible(true);
			}
		});
		button_1.setBounds(26, 209, 116, 23);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("Alterar");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Usuario usuario = new Usuario();
				usuario = (Usuario) cbbUsername.getSelectedItem();
				cadastraUsuario.AbreTela(2, usuario);
				cadastraUsuario.setVisible(true);
			}
		});
		button_2.setBounds(187, 209, 89, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("Deletar");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Usuario usuario = new Usuario();
				usuario =(Usuario) cbbUsername.getSelectedItem();
				dadosUsuario.deleteUsuario(usuario);
				
			}
		});
		button_3.setBounds(314, 209, 89, 23);
		contentPane.add(button_3);
	}

}
